package com.settlement.system.controller;

import com.settlement.system.common.enums.ReturnStatusEnum;
import com.settlement.system.model.entity.common.BasePager;
import com.settlement.system.model.entity.common.SinglePage;
import com.settlement.system.model.entity.common.TblWpAuditStaticDtlSummary;
import com.settlement.system.reply.ObjectDataResponse;
import com.settlement.system.service.IBaseService;
import com.settlement.system.service.ITblWpAuditCloseAccountService;
import com.settlement.system.service.ITblWpAuditStaticDtlSummaryService;
import com.settlement.system.common.util.common.DateUtil;
import com.settlement.system.common.util.common.FasUtil;
import com.settlement.system.common.util.common.StringMathUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * wp-稽核汇总 前端控制器
 *
 * @author xiaochuan
 * @since 2020-11-25
 */

@Api(tags = "wp-稽核明细汇总", description = "wp-稽核明细汇总")
@RestController
@RequestMapping("/wp/audit/dtl/summary")
@Slf4j
public class TblWpAuditStaticDtlSummaryController extends ExcelController<TblWpAuditStaticDtlSummary> {

    @Resource
    private ITblWpAuditStaticDtlSummaryService tblWpAuditStaticDtlSummaryService;

    @Resource
    private ITblWpAuditCloseAccountService tblWpAuditCloseAccountService;

    @Override
    public Class<?> getModelClass() {
        return TblWpAuditStaticDtlSummary.class;
    }

    @Override
    public IBaseService init() {
        return tblWpAuditStaticDtlSummaryService;
    }

    @Override
    @ApiOperation("分页查询")
    @RequestMapping(value = "list", method = {RequestMethod.POST})
    @ResponseBody
    protected ObjectDataResponse getPageList(HttpServletRequest request, @RequestBody BasePager<Map<String, Object>> model) {
        Map<String, Object> result = new HashMap<>(16);
        try {
            setParamsModel(model.getModel());

            String verifition = verifyRequest(model.getModel());
            if (!StringUtils.isEmpty(verifition)) {
                return new ObjectDataResponse(ReturnStatusEnum.CHECK_FAILURE.getValue(), verifition);
            }

            List<TblWpAuditStaticDtlSummary> rows = tblWpAuditStaticDtlSummaryService.selectByPage(new SinglePage(model), model.getModel(),
                    model.getOrderByField(), model.getOrderBy());
            TblWpAuditStaticDtlSummary foot = tblWpAuditStaticDtlSummaryService.getFoot(model.getModel());
            result.put("rows", rows);
            if (null != foot) {
                result.put("total", foot.getCountSum());
                result.put("foot", foot);
            } else {
                result.put("total", 0);
                result.put("foot", new TblWpAuditStaticDtlSummary());
            }
        } catch (Exception ex) {
            log.error("获取列表出错", ex);
            return new ObjectDataResponse(500, "获取列表失败，请联系管理员！");
        }
        return new ObjectDataResponse<Map>(result, 200, "获取列表成功");
    }

    private String verifyRequest(Map<String, Object> params) {
        String sendDate = params.get("sendDate") != null ? params.get("sendDate").toString() : "";
        String billDate = params.get("billDate") != null ? params.get("billDate").toString() : "";
        String rssDate = params.get("rssDate") != null ? params.get("rssDate").toString() : "";
        String sendInitDate = params.get("sendInitDate") != null ? params.get("sendInitDate").toString() : "";
        String billInitDate = params.get("billInitDate") != null ? params.get("billInitDate").toString() : "";
        String rssInitDate = params.get("rssInitDate") != null ? params.get("rssInitDate").toString() : "";
        String shopNos = params.get("shopNos") != null ? params.get("shopNos").toString() : "";


        if (!StringUtils.isEmpty(sendInitDate)
                && sendDate.compareTo(sendInitDate) < 0) {
            return "发退货截至日期不能小于起始日期";
        }

        if (!StringUtils.isEmpty(billInitDate)
                && billDate.compareTo(billInitDate) < 0) {
            return "账单截至日期不能小于起始日期";
        }

        if (!StringUtils.isEmpty(rssInitDate)
                && rssDate.compareTo(rssInitDate) < 0) {
            return "收发存截至日期不能小于起始日期";
        }

        try {
            Date zeroDate = DateUtil.getDate("2000-01-01", "yyyy-MM-dd");

            Date maxInitialDate = tblWpAuditCloseAccountService.getMaxInitialDateByShopNo(shopNos);
            if(StringUtils.isNotBlank(sendInitDate)){
                Date sendInitDate1 = DateUtil.getDate(sendInitDate, "yyyy-MM-dd");
                if(zeroDate.compareTo(sendInitDate1)!=0 && sendInitDate1.compareTo(maxInitialDate)<=0){
                    return "起始日期必须大于店铺初始日期。";
                }
            }
            if(StringUtils.isNotBlank(billInitDate)){
                Date billInitDate1 = DateUtil.getDate(billInitDate, "yyyy-MM-dd");
                if(zeroDate.compareTo(billInitDate1)!=0 && billInitDate1.compareTo(maxInitialDate)<=0){
                    return "起始日期必须大于店铺初始日期。";
                }
            }
            if(StringUtils.isNotBlank(rssInitDate)){
                Date rssInitDate1 = DateUtil.getDate(rssInitDate, "yyyy-MM-dd");
                if(zeroDate.compareTo(rssInitDate1)!=0 && rssInitDate1.compareTo(maxInitialDate)<=0){
                    return "起始日期必须大于店铺初始日期。";
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ReturnStatusEnum.REQUEST_ABNORMAL.getText();
        }

        return null;
    }

    @Override
    protected void setParamsModel(Map<String, Object> model) {
        FasUtil.formatInQueryCondition(model, "unBalanceQtyType", "unBalanceAmountType", "unSettledBackDiffQtyType",
                "unSettledBackDiffAmountType", "unSettledRefundDiffQtyType",
                "unSettledRefundDiffAmountType", "unSettledOverduefQtyType",
                "unSettledOverdueAmountType", "diffAmountType", "shopNos","orderSchedule","poNo","deliveryType");


        String sendDate = (String) model.get("sendDate");
        String billDate = (String) model.get("billDate");
        String rssDate = (String) model.get("rssDate");

        String sendInitDate = (String) model.get("sendInitDate");
        if(StringUtils.isEmpty(sendInitDate)){
            model.put("sendInitDate","2000-01-01");
        }
        String billInitDate = (String) model.get("billInitDate");
        if(StringUtils.isEmpty(billInitDate)){
            model.put("billInitDate","2000-01-01");
        }
        String rssInitDate = (String) model.get("rssInitDate");
        if(StringUtils.isEmpty(rssInitDate)){
            model.put("rssInitDate","2000-01-01");
        }

        model.put("minSearchDate", StringMathUtils.min(sendDate, billDate, rssDate));
        model.put("maxSearchDate", StringMathUtils.max(sendDate, billDate, rssDate));
        super.setParamsModel(model);
    }
}

